package com.lc.advertising.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.advertising.R;
import com.lc.advertising.app.AppActivity;
import com.lc.advertising.http.request.LoginApi;
import com.lc.advertising.http.response.LoginBean;
import com.lc.advertising.manager.InputTextManager;
import com.lc.advertising.other.IntentKey;
import com.lc.advertising.util.SPUtil;
import com.lc.advertising.widget.ClearEditText;
import com.lc.advertising.widget.PasswordEditText;

import androidx.appcompat.widget.AppCompatTextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends AppActivity {
    @BindView(R.id.et_login_phone)
    ClearEditText etLoginPhone;
    @BindView(R.id.et_login_password)
    PasswordEditText etLoginPassword;
    @BindView(R.id.tv_login_forget)
    AppCompatTextView tvLoginForget;
    @BindView(R.id.btn_login_commit)
    Button btnLoginCommit;
    @BindView(R.id.ll_register)
    LinearLayout llRegister;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        InputTextManager.with(this)
                .addView(etLoginPhone)
                .addView(etLoginPassword)
                .setMain(btnLoginCommit)
                .build();
    }

    @OnClick({R.id.btn_login_commit, R.id.ll_register,R.id.tv_login_forget})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_login_commit:
//                startActivity(MainActivity.class);
//                finish();
                login();

                break;
            case R.id.ll_register:
                startActivity(RegisterActivity.class);
                break;
            case R.id.tv_login_forget:
                startActivity(new Intent(this,ForgetPwdActivity.class));

                break;
        }
    }

    private void login() {
        EasyHttp.post(this)
                .api(new LoginApi()
                        .setMobile(etLoginPhone.getText().toString())
                        .setPassword(etLoginPassword.getText().toString()))
                .request(new HttpCallback<LoginBean>(this) {



                    @Override
                    public void onSucceed(LoginBean data) {
                        toast(data.getMessage());
                      if (data.getCode()==200){
                          if (data.getIs_renzheng().equals("0")){
                              startActivity(AttestationActivity.class);
                          }else {
                              startActivity(MainActivity.class);
                          }


                          SPUtil.put(LoginActivity.this, IntentKey.UID,data.getUid()+"");
                          finish();

                      }


                    }

                });
    }
}
